package org.stars.config;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;


/**
 * Implementation of MessageBundle interface. This class load labels from
 * file stored in package org/stars/config.
 * 
 * @author Francesco Benincasa
 *
 */
public class MessageBundleImpl implements MessageBundle {
	
	/** The file name. */
	protected String fileName="org/stars/config/Stars_Messages";
	
	/** The messages. */
	protected ResourceBundle messages;
	
	/** The current locale. */
	protected Locale currentLocale;
	
	/* (non-Javadoc)
	 * @see org.stars.config.MessageBundle#load()
	 */
	@Override
	public void load()
	{		
		load(Locale.ENGLISH);		
	}
	
	/* (non-Javadoc)
	 * @see org.stars.config.MessageBundle#load(java.util.Locale)
	 */
	@Override
	public void load(Locale locale)
	{		
		currentLocale=locale;
		messages= ResourceBundle.getBundle(fileName, currentLocale);		
	}
			
	/**
	 * Gets the message.
	 *
	 * @param id the id
	 * @param args the args
	 * @return the message
	 */
	public String getMessage(String id, String ...  args )
	{
		Object[] messageArguments = args;		
		
		MessageFormat formatter = new MessageFormat("");
		formatter.setLocale(currentLocale);
		
		formatter.applyPattern(messages.getString(id));
		String output = formatter.format(messageArguments);
		 
		return output;
	}

	/* (non-Javadoc)
	 * @see org.stars.config.MessageBundle#getMessage(java.lang.Object, java.lang.String[])
	 */
	@Override
	public String getMessage(Object message, String... args) {
		return getMessage(message.toString(), args);
	}

	/* (non-Javadoc)
	 * @see org.stars.config.MessageBundle#getCurrentLocale()
	 */
	@Override
	public Locale getCurrentLocale() {
		return currentLocale;
	}

	/* (non-Javadoc)
	 * @see org.stars.config.MessageBundle#setCurrentLocale(java.util.Locale)
	 */
	@Override
	public void setCurrentLocale(Locale value) {
		currentLocale=value;		
	}

	/**
	 * Gets the file name.
	 *
	 * @return the file name
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * Sets the file name.
	 *
	 * @param value the new file name
	 */
	public void setFileName(String value) {
		fileName = value;
	}
}
